package com.ruoyi.smart.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.smart.config.CouchDBService;
import com.ruoyi.smart.domain.*;
import com.ruoyi.smart.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.smart.service.ISmartProductTestAllDataService;

/**
 * 产品测试数据集Service业务层处理
 * 
 * @author minwei
 * @date 2023-09-04
 */
@Service
public class SmartProductTestAllDataServiceImpl implements ISmartProductTestAllDataService 
{
    @Autowired
    private CouchDBService couchDBService;
    @Autowired
    private SmartProductTestAllDataMapper smartProductTestAllDataMapper;
    @Autowired
    private SmartProductMapper smartProductMapper;
    @Autowired
    private SmartProductTestMapper smartProductTestMapper;
    @Autowired
    private SmartProductTestRecordMapper smartProductTestRecordMapper;
    @Autowired
    private SmartProductTestWaveformMapper smartProductTestWaveformMapper;


    /**
     * 查询产品测试数据集
     * 
     * @param id 产品测试数据集主键
     * @return 产品测试数据集
     */
    @Override
    public SmartProductTestAllData selectSmartProductTestAllDataById(Long id)
    {
        return smartProductTestAllDataMapper.selectSmartProductTestAllDataById(id);
    }


    public void searchData(){
        List<SmartProduct> smartProducts=smartProductMapper.selectSmartProductListTop1000(new SmartProduct());
        while (smartProducts.size()>0){
            for(SmartProduct smartProduct : smartProducts){
                SmartProductTest smartProductTest=new SmartProductTest();
                smartProductTest.setProductId(smartProduct.getCode());
                List<SmartProductTest> smartProductTests=smartProductTestMapper.selectSmartProductTestList(smartProductTest);
                for(SmartProductTest item : smartProductTests){
                    SmartProductTestAllData temp =new SmartProductTestAllData();
                    temp.setProductId(smartProduct.getId());
                    temp.setProductCode(smartProduct.getCode());
                    temp.setTestResult(item.getTestResult());
                    temp.setTestTime(item.getTestTime());
                    temp.setCurrent(item.getCurrent());
                    temp.setVoltage(item.getVoltage());
                    temp.setTemperature(item.getTemperature());
                    temp.setShakeTime(item.getShakeTime());
                    temp.setShakeFrequency(item.getShakeFrequency());
                    temp.setShakeSize(item.getShakeSize());
                    temp.setDeviceId(item.getDeviceId());
                    temp.setCreateDate(item.getCreateDate());
                    temp.setTestName(item.getState());
                    temp.setTestUesr(item.getUserId());
                    SmartProductTestWaveform smartProductTestWaveform=new SmartProductTestWaveform();
                    smartProductTestWaveform.setProductCode(smartProduct.getCode());
                    smartProductTestWaveform.setNotes(item.getState());
                    List<SmartProductTestWaveform> smartProductTestWaveforms=smartProductTestWaveformMapper.selectSmartProductTestWaveformList(smartProductTestWaveform);
                    if(smartProductTestWaveforms.size()>0){
                        temp.setWaveData(smartProductTestWaveforms.get(0).getData());
                    }
                    smartProductTestAllDataMapper.insertSmartProductTestAllData(temp);
                }
                smartProduct.setReadState("1");
                smartProductMapper.updateSmartProduct(smartProduct);
            }
        }

    }

    /**
     * 查询产品测试数据集列表
     * 
     * @param smartProductTestAllData 产品测试数据集
     * @return 产品测试数据集
     */
    @Override
    public List<SmartProductTestAllData> selectSmartProductTestAllDataList(SmartProductTestAllData smartProductTestAllData)
    {
        List<SmartProductTestAllData> list=smartProductTestAllDataMapper.selectSmartProductTestAllDataList(smartProductTestAllData);
        for(SmartProductTestAllData item : list){
            item.setWaveData(couchDBService.getString(item.getWaveData()));
        }
        return list;
    }

    /**
     * 新增产品测试数据集
     * 
     * @param smartProductTestAllData 产品测试数据集
     * @return 结果
     */
    @Override
    public int insertSmartProductTestAllData(SmartProductTestAllData smartProductTestAllData)
    {
        smartProductTestAllData.setWaveData(couchDBService.insert(smartProductTestAllData.getWaveData()));
        return smartProductTestAllDataMapper.insertSmartProductTestAllData(smartProductTestAllData);
    }

    /**
     * 修改产品测试数据集
     * 
     * @param smartProductTestAllData 产品测试数据集
     * @return 结果
     */
    @Override
    public int updateSmartProductTestAllData(SmartProductTestAllData smartProductTestAllData)
    {
        smartProductTestAllData.setWaveData(couchDBService.insert(smartProductTestAllData.getWaveData()));
        return smartProductTestAllDataMapper.updateSmartProductTestAllData(smartProductTestAllData);
    }

    /**
     * 批量删除产品测试数据集
     * 
     * @param ids 需要删除的产品测试数据集主键
     * @return 结果
     */
    @Override
    public int deleteSmartProductTestAllDataByIds(Long[] ids)
    {
        List<SmartProductTestAllData> list=smartProductTestAllDataMapper.selectSmartProductTestAllDataByIds(ids);
        for(SmartProductTestAllData item : list){
            couchDBService.delete(item.getWaveData());
        }
        return smartProductTestAllDataMapper.deleteSmartProductTestAllDataByIds(ids);
    }

    /**
     * 删除产品测试数据集信息
     * 
     * @param id 产品测试数据集主键
     * @return 结果
     */
    @Override
    public int deleteSmartProductTestAllDataById(Long id)
    {
        SmartProductTestAllData smartProductTestAllData=smartProductTestAllDataMapper.selectSmartProductTestAllDataById(id);
        couchDBService.delete(smartProductTestAllData.getWaveData());
        return smartProductTestAllDataMapper.deleteSmartProductTestAllDataById(id);
    }
}
